using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
using cb0tServer;

namespace sb0t_winGUI
{
    public partial class Form2 : Form
    {
        private bool drag = false;
        private Point pos = new Point();

        public Form2()
        {
            InitializeComponent();

            CommandLevels.Load();

            String[] cmds = CommandLevels.GetCommands(1);
            if (cmds != null)
                foreach (String str in cmds)
                    listBox1.Items.Add(str);

            cmds = CommandLevels.GetCommands(2);
            if (cmds != null)
                foreach (String str in cmds)
                    listBox2.Items.Add(str);

            cmds = CommandLevels.GetCommands(3);
            if (cmds != null)
                foreach (String str in cmds)
                    listBox3.Items.Add(str);

            cmds = CommandLevels.GetCommands(4);
            if (cmds != null)
                foreach (String str in cmds)
                    listBox4.Items.Add(str);
        }

        private void button9_Click(object sender, EventArgs e)
        {
            foreach (object o in listBox1.Items)
                CommandLevels.SetLevel((String)o, 1);

            foreach (object o in listBox2.Items)
                CommandLevels.SetLevel((String)o, 2);

            foreach (object o in listBox3.Items)
                CommandLevels.SetLevel((String)o, 3);

            foreach (object o in listBox4.Items)
                CommandLevels.SetLevel((String)o, 4);

            CommandLevels.Save();

            this.Hide();
        }

        #region form_appearance
        protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs e)
        {
            base.OnPaintBackground(e);

            Graphics g = e.Graphics;
            Rectangle r = new Rectangle(0, 0, this.Width, this.Height);

            for (int i = 2; i < 10; i++)
            {
                g.DrawRectangle(new Pen(Color.FromArgb((i * 25), (i * 25), (i * 25)), 1), r);
                r = new Rectangle(i - 2, i - 2, this.Width - (2 * (i - 2)), this.Height - (2 * (i - 2)));
            }

            LinearGradientBrush l = new LinearGradientBrush(r,
                Color.FromArgb(210, 210, 210),
                Color.FromArgb(255, 255, 255),
                45f);

            g.FillRectangle(l, r);

            g.DrawLine(new Pen(Brushes.DarkGray, 2), new Point(6, 32), new Point(this.Width - 5, 32));

            r = new Rectangle(6, 7, this.Width - 12, 24);
            g.FillRectangle(Brushes.LightGray, r);

            Font f = new System.Drawing.Font("Verdana", 10F, System.Drawing.FontStyle.Regular,
                System.Drawing.GraphicsUnit.Point, ((byte)(0)));

            g.DrawString("Customise command levels", f, Brushes.Black, new PointF(10, 10));

            l.Dispose();

            this.Opacity = 0.90;
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);

            if (this.drag)
            {
                Point p1 = new Point(e.X, e.Y);
                Point p2 = this.PointToScreen(p1);
                Point p3 = new Point(p2.X - this.pos.X,
                                     p2.Y - this.pos.Y);
                this.Location = p3;
            }
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);

            this.drag = false;
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);

            if (e.X >= 6 && e.X <= (this.Width - 7))
            {
                if (e.Y >= 6 && e.Y <= 32)
                {
                    this.drag = true;
                    this.pos = new Point(e.X, e.Y);
                }
            }
        }
        #endregion

        private void listBox1_Click(object sender, EventArgs e)
        {
            listBox2.SelectedItem = null;
            listBox3.SelectedItem = null;
            listBox4.SelectedItem = null;
            button1.Enabled = false;
            button2.Enabled = false;
            button3.Enabled = false;
            button4.Enabled = false;
            button5.Enabled = false;
            button6.Enabled = false;

            if (listBox1.SelectedItem != null)
            {
                button2.Enabled = true;
            }
        }

        private void listBox2_Click(object sender, EventArgs e)
        {
            listBox1.SelectedItem = null;
            listBox3.SelectedItem = null;
            listBox4.SelectedItem = null;
            button1.Enabled = false;
            button2.Enabled = false;
            button3.Enabled = false;
            button4.Enabled = false;
            button5.Enabled = false;
            button6.Enabled = false;

            if (listBox2.SelectedItem != null)
            {
                button1.Enabled = true;
                button3.Enabled = true;
            }
        }

        private void listBox3_Click(object sender, EventArgs e)
        {
            listBox1.SelectedItem = null;
            listBox2.SelectedItem = null;
            listBox4.SelectedItem = null;
            button1.Enabled = false;
            button2.Enabled = false;
            button3.Enabled = false;
            button4.Enabled = false;
            button5.Enabled = false;
            button6.Enabled = false;

            if (listBox3.SelectedItem != null)
            {
                button4.Enabled = true;
                button5.Enabled = true;
            }
        }

        private void listBox4_Click(object sender, EventArgs e)
        {
            listBox1.SelectedItem = null;
            listBox2.SelectedItem = null;
            listBox3.SelectedItem = null;
            button1.Enabled = false;
            button2.Enabled = false;
            button3.Enabled = false;
            button4.Enabled = false;
            button5.Enabled = false;
            button6.Enabled = false;

            if (listBox4.SelectedItem != null)
            {
                button6.Enabled = true;
            }
        }

        private void button2_Click(object sender, EventArgs e) // 1 -> 2
        {
            listBox2.Items.Add(listBox1.SelectedItem.ToString());
            listBox2.SelectedItem = listBox2.Items[listBox2.Items.Count - 1];
            listBox1.Items.Remove(listBox1.SelectedItem);
            button2.Enabled = false;
            button1.Enabled = true;
            button3.Enabled = true;
        }

        private void button1_Click(object sender, EventArgs e) // 2 -> 1
        {
            listBox1.Items.Add(listBox2.SelectedItem.ToString());
            listBox1.SelectedItem = listBox1.Items[listBox1.Items.Count - 1];
            listBox2.Items.Remove(listBox2.SelectedItem);
            button1.Enabled = false;
            button3.Enabled = false;
            button2.Enabled = true;
        }

        private void button3_Click(object sender, EventArgs e) // 2 -> 3
        {
            listBox3.Items.Add(listBox2.SelectedItem.ToString());
            listBox3.SelectedItem = listBox3.Items[listBox3.Items.Count - 1];
            listBox2.Items.Remove(listBox2.SelectedItem);
            button1.Enabled = false;
            button3.Enabled = false;
            button4.Enabled = true;
            button5.Enabled = true;
        }

        private void button4_Click(object sender, EventArgs e) // 3 -> 2
        {
            listBox2.Items.Add(listBox3.SelectedItem.ToString());
            listBox2.SelectedItem = listBox2.Items[listBox2.Items.Count - 1];
            listBox3.Items.Remove(listBox3.SelectedItem);
            button4.Enabled = false;
            button5.Enabled = false;
            button1.Enabled = true;
            button3.Enabled = true;
        }

        private void button5_Click(object sender, EventArgs e) // 3 -> 4
        {
            listBox4.Items.Add(listBox3.SelectedItem.ToString());
            listBox4.SelectedItem = listBox4.Items[listBox4.Items.Count - 1];
            listBox3.Items.Remove(listBox3.SelectedItem);
            button4.Enabled = false;
            button5.Enabled = false;
            button6.Enabled = true;
        }

        private void button6_Click(object sender, EventArgs e) // 4 -> 3
        {
            listBox3.Items.Add(listBox4.SelectedItem.ToString());
            listBox3.SelectedItem = listBox3.Items[listBox3.Items.Count - 1];
            listBox4.Items.Remove(listBox4.SelectedItem);
            button4.Enabled = true;
            button5.Enabled = true;
            button6.Enabled = false;
        }

    }
}